package com.pn.controller;

import com.pn.entity.ProductType;
import com.pn.service.ProductService;
import com.pn.service.ProductTypeService;
import com.pn.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@RestController
@RequestMapping("productCategory")
public class productTypeController {
    @Autowired
    private ProductService productService;

    @Autowired
    private ProductTypeService productTypeService;

    //显示商品分类树
    @GetMapping("/product-category-tree")
    public Result productCategoryTree(){
        List<ProductType> productTypes = productService.categoryTree();
        return Result.ok(productTypes);
    }

    //根据分类编码验证商品分类是否存在
    @GetMapping("/verify-type-code")
    public Result verifyTypeCode(String typeCode){
        Result result = productTypeService.checkTypeCode(typeCode);
        return result;
    }

    //添加商品分类
    @PostMapping("/type-add")
    public Result addProductType(@RequestBody ProductType productType){
        Result result = productService.saveProductType(productType);
        return result;
    }

    //删除商品分类
    @DeleteMapping("/type-delete/{typeId}")
    public Result deleteProductType(@PathVariable Integer typeId){
        Result result = productService.deleteTypeByTidOrPid(typeId,typeId);
        return result;
    }

    //修改商品分类
    @PutMapping("type-update")
    public Result typeUpdate(@RequestBody ProductType productType){
        return productService.typeUpdate(productType);
    }


}
